package com.ushine.web.component.mybatis.desensitization.plugin;

import com.ushine.web.component.mybatis.desensitization.DesensitizationServiceImpl;
import com.ushine.web.component.mybatis.desensitization.SensitiveTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @Author: yls
 * @Date 2022/2/11 16:48
 * @Description: 字段脱敏配置
 * @Version 1.0
 **/
@Service
public class DesensitizationFunManager {
    @Resource
    private DesensitizationServiceImpl desensitizationService;

    /**
     * 将给定的参数应用到这个函数上,
     * 传入的参数类型为T返回类型为R
     * <p>
     * Function<T,R>
     */
    private EnumMap<SensitiveTypeEnum, Function<String, String>> functionTypeMap = new EnumMap<>(SensitiveTypeEnum.class);


    @PostConstruct
    public void initMapFunctionService() {
        // 用户名脱敏
        functionTypeMap.put(SensitiveTypeEnum.CHINESE_NAME, params -> desensitizationService.hiddenChineseName(params));
        // 地址
        functionTypeMap.put(SensitiveTypeEnum.ADDRESS, params -> desensitizationService.hiddenAddress(params));
        // 身份证号脱敏
        functionTypeMap.put(SensitiveTypeEnum.ID_CARD, params -> desensitizationService.hiddenIdCard(params));
        // 银行卡号脱敏
        functionTypeMap.put(SensitiveTypeEnum.BANK_CARD, params -> desensitizationService.hiddenBankCard(params));
        // 车牌号脱敏
        functionTypeMap.put(SensitiveTypeEnum.CAR_NUMBER, params -> desensitizationService.hiddenCarNumber(params));
        // 邮箱脱敏
        functionTypeMap.put(SensitiveTypeEnum.EMAIL, params -> desensitizationService.hiddenEmail(params));
        // 固定电话脱敏
        functionTypeMap.put(SensitiveTypeEnum.FIXED_PHONE, params -> desensitizationService.hiddenFixedPhone(params));
        // 移动电话脱敏
        functionTypeMap.put(SensitiveTypeEnum.MOBILE_PHONE, params -> desensitizationService.hiddenPhone(params));

    }

    /**
     * 功能描述:
     * 〈 具体的业务逻辑调用 〉
     *
     * @param typeName 类型名称
     * @param params   参数列表
     * @return : java.lang.String
     * @author : yls
     * @date : 2022/2/8 13:45
     */
    public String getResultByTypeName(SensitiveTypeEnum typeName, String params) {
        Function<String, String> function = functionTypeMap.get(typeName);
        if (function != null) {
            return function.apply(params);
        }
        return "未找到对应类型的业务处理";
    }
}
